<?php

require_once('_Core.php');

function phptal_tales_block($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return '$ctx->this->objectRender(array(\'Type.slug\' => \'content-block\', \'slug\' => '.PHPTAL_Php_TalesInternal::string($src, $pNoThrow).'), \'view\')';
}

class SilkCore_BlockMeta_CustomPhp extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Custom PHP';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }

}

class SilkCore_Type_Block extends SilkCore_Type_Base {

    public function __construct(){
	$this->_meta = array(
	    new SilkCore_BlockMeta_CustomPhp()
	);
    }

    public function getName(){
	return 'Content Block';
    }

    public function getSlug(){
	return 'content-block';
    }

    public function getDescription(){
	return 'Global content block';
    }

    public function render(Silk_Object $pBlock=null, $pAction=null, array $pVars=array()){
	$view = $this->prepare($pBlock, $pVars, $pAction);

	if ($pAction instanceof Silk_Template)
	    return parent::render($pBlock, $pAction, $pVars);
	else if ($pAction){

	    $action = strtolower($pAction);
	    switch ($action){
		case 'admin':
		    return $this->_admin($view);
		case 'create':
		    return $this->_create($view, $pBlock);
		case 'view':
		    return $this->_view($view, $pBlock);
		case 'edit':
		    return $this->_edit($view, $pBlock);
		case 'delete':
		    return $this->_delete($view, $pBlock);
	    }

	} else {
// 	    $view = $this->prepare($pBlock, $pVars);
	    return $this->renderString($view, $pBlock->content);
	}
    }

    protected function prepare(Silk_Object $pBlock=null, array $pVars=array(), $pAction=null){
	$view = $this->getViewEngine();

	if ($pBlock){
	    $view->assign('block', $pBlock);
	    $view->assign('object', $pBlock);
	}


	foreach ($pVars as $key => $val){
	    $view->assign($key, $val);
	}

	if ($pBlock){
	    $php = $pBlock->getMetaValue('Custom PHP');

	    if ($php){

		$func = sprintf('silkfunc__page__%s_%d_%d', $pPage->getSlug(), time(), uniqid());
		eval(sprintf('function %s(&$VIEW, $PARAMS, $ACTION) { %s }', $func, $php));

		$func($view, $_REQUEST, $pAction);

	    }
	}

	return $view;
    }

    public function _view(Zend_View_Interface $pView, Silk_Object $pBlock){
	return $this->renderString($pView, $pBlock->content);
    }

    protected function _create(Zend_View_Interface $pView, Silk_Object $pBlock){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	$pView->this->appendScript($this, 'js/core.manage.js');
	return $this->renderTemplate($pView, 'edit-block');

    }

    protected function _edit(Zend_View_Interface $pView, Silk_Object $pBlock){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	$pView->this->appendScript($this, 'js/core.manage.js');
	return $this->renderTemplate($pView, 'edit-block');

    }

    protected function _admin(Zend_View_Interface $pView){
	if (array_key_exists('sort', $_REQUEST)){
	    $sort = $_REQUEST['sort'];
	} else
	    $sort = 'object_id';

	if (array_key_exists('order', $_REQUEST)){
	    $order = $_REQUEST['order'];
	} else
	    $order = 'ASC';

	if (array_key_exists('limit', $_REQUEST)){
	    $limit = $_REQUEST['limit'];
	} else
	    $limit = 50;

	if (array_key_exists('page', $_REQUEST)){
	    $page = $_REQUEST['page'];
	} else
	    $page = 1;

	$paginator = Silk::getObjectPaginator($this->getSlug(), array(), $sort, $order);

	$paginator->setItemCountPerPage($limit);
	$paginator->setCurrentPageNumber($page);

	$Types = new Silk_Types();
	$type = $Types->fetchBySlug($this->getSlug(), true);

	$pView->moduletype = $this;
	$pView->type = $type;
	$pView->blocks = $paginator;
	$pView->limit = $limit;
	$pView->sort  = $sort;
	$pView->order = $order;
	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'block-admin');
    }
}

?>